import fly from './interceptors.js'

export const wxRequest = (params = {}, url, method = 'GET', headers = {}) => {
	// put等类型 参数拼接
	let data = params
	let pathParams = url.match(/\{(.*)\}/)
	if (pathParams) {
		for (var i = 0; i < pathParams.length; i++) {
			let paramKey = pathParams[i].replace('{', '').replace('}', '')
			paramKey = paramKey.split('/')
			url = url.replace(paramKey[i], params[paramKey[i]])
			url = url.replace('{', '').replace('}', '')
			delete data[paramKey]
		}
	}
	return new Promise((resolve, reject) => {
		fly.request(url, data, {
			method,
			headers
		}).then(ret => {
			
			if (ret.data.errorCode == 0) {
				resolve(ret)
			} else {
				var error = {
					code: ret.data.errorCode,
					message: ret.data.errorMsg
				}
				uni.showToast({
					title: error.message,
					icon: 'none',
					duration: 2000
				})
				console.log(error,'showToast');
				reject(error)
				
			}
		}, err => {
		})

	})
}
